<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns:yui="http://yuilibrary.com/rdf/1.0/yui.rdf#">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	<title>API: profiler   profiler.js  (YUI Library)</title>
	
	<link rel="shortcut icon" href="assets/yui_icon.ico" type="image/x-icon" />
	<link rel="stylesheet" type="text/css" href="assets/reset-fonts-grids-min.css" />
	<link rel="stylesheet" type="text/css" href="assets/api.css" />

    <script type="text/javascript" src="assets/api-js"></script>
    <script type="text/javascript" src="assets/ac-js"></script>
</head>

<body id="yahoo-com">

<div id="doc3" class="yui-t2">
	<div id="hd">
        <h1>
        	<a href="http://developer.yahoo.com/yui/" title="Yahoo! UI Library">Yahoo! UI Library</a>
        	<img src="assets/yui_icon.png" alt="YUI文档中文化" />
        </h1>
        <h3>profiler&nbsp; <span class="subtitle">2.7.0b</span></h3>
        <a href="./index.html" title="Yahoo! UI Library">Yahoo! UI Library</a> 
            &gt; <a href="./module_profiler.html" title="profiler">profiler</a>
                
                 &gt; profiler.js (source view) 
        <form onsubmit="return false">
            <div id="propertysearch">
                搜索: <input autocomplete="off" id="searchinput" />
                <div id="searchresults">
                    &nbsp;
                </div>
            </div>
        </form>
	</div>

	<div id="bd">
		<div id="yui-main">
			<div class="yui-b">
            <form action="#" name="yui-classopts-form" method="get" id="yui-classopts-form">
                <fieldset>
                    <legend>Filters</legend>
                <span class="classopts"><input type="checkbox" name="show_private" id="show_private" /> <label for="show_private">Show Private</label></span>
                <span class="classopts"><input type="checkbox" name="show_protected" id="show_protected" /> <label for="show_protected">Show Protected</label></span>
                <span class="classopts"><input type="checkbox" name="show_deprecated" id="show_deprecated" /> <label for="show_deprecated">Show Deprecated</label></span>
                </fieldset>
            </form>

                    <div id="srcout">
                        <style>
                            #doc3 .classopts { display:none; }
                        </style>
<div class="highlight"><pre><span class="cm">/*</span>
<span class="cm">Copyright (c) 2009, Yahoo! Inc. All rights reserved.</span>
<span class="cm">Code licensed under the BSD License:</span>
<span class="cm">http://developer.yahoo.net/yui/license.txt</span>
<span class="cm">version: 2.7.0</span>
<span class="cm">*/</span>

<span class="cm">/**</span>
<span class="cm"> * YUI JavaScript 性能评测器。</span>
<span class="cm"> * @module profiler</span>
<span class="cm"> * @namespace YAHOO.tool</span>
<span class="cm"> * @requires yahoo</span>
<span class="cm"> */</span>
<span class="nx">YAHOO</span><span class="p">.</span><span class="nx">namespace</span><span class="p">(</span><span class="s2">&quot;tool&quot;</span><span class="p">);</span>

<span class="cm">/**</span>
<span class="cm"> * JavaScript 的性能评测类。</span>
<span class="cm"> * @namespace YAHOO.tool</span>
<span class="cm"> * @class Profiler</span>
<span class="cm"> * @static</span>
<span class="cm"> */</span>
<span class="nx">YAHOO</span><span class="p">.</span><span class="nx">tool</span><span class="p">.</span><span class="nx">Profiler</span> <span class="o">=</span> <span class="p">{</span>

    <span class="c1">//-------------------------------------------------------------------------</span>
    <span class="c1">// 私有属性</span>
    <span class="c1">//-------------------------------------------------------------------------</span>

    <span class="cm">/**</span>
<span class="cm">     * 用来存放未经评测的原始方法的容器对象。</span>
<span class="cm">     * @type Object</span>
<span class="cm">     * @private</span>
<span class="cm">     * @static</span>
<span class="cm">     * @property _container</span>
<span class="cm">     */</span>
    <span class="nx">_container</span> <span class="o">:</span> <span class="k">new</span> <span class="nb">Object</span><span class="p">(),</span>

    <span class="cm">/**</span>
<span class="cm">     * 函数的调用信息。</span>
<span class="cm">     * （译注：这里的“函数”也可能是对象的方法，下同。）</span>
<span class="cm">     * @type Object</span>
<span class="cm">     * @private</span>
<span class="cm">     * @static</span>
<span class="cm">     * @property _report</span>
<span class="cm">     */</span>
    <span class="nx">_report</span> <span class="o">:</span> <span class="k">new</span> <span class="nb">Object</span><span class="p">(),</span>
    
    <span class="c1">//-------------------------------------------------------------------------</span>
    <span class="c1">// 私有方法</span>
    <span class="c1">//-------------------------------------------------------------------------</span>
    
    <span class="cm">/**</span>
<span class="cm">     * 当一个方法执行完成时将执行本方法。</span>
<span class="cm">     * 记录本方法执行的起止时间并进而计算出某函数执行所耗费的时间。</span>
<span class="cm">     * 它还计算该函数的最短、最长和平均耗时。</span>
<span class="cm">     * @param {String} name 函数名称，该函数将被标记为“执行完毕”.</span>
<span class="cm">     * @param {int} duration 该函数执行所耗费的时间（单位：毫秒）。</span>
<span class="cm">     * @return {Void}</span>
<span class="cm">     * @private</span>
<span class="cm">     * @static</span>
<span class="cm">     */</span>
    <span class="nx">_saveData</span> <span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">name</span> <span class="cm">/*:String*/</span><span class="p">,</span> <span class="nx">duration</span> <span class="cm">/*:int*/</span><span class="p">){</span>
        
        <span class="c1">//获取函数数据</span>
        <span class="kd">var</span> <span class="nx">functionData</span> <span class="cm">/*:Object*/</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_report</span><span class="p">[</span><span class="nx">name</span><span class="p">];</span>
    
        <span class="c1">//增加调用的数量</span>
        <span class="nx">functionData</span><span class="p">.</span><span class="nx">calls</span><span class="o">++</span><span class="p">;</span>
        <span class="nx">functionData</span><span class="p">.</span><span class="nx">points</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">duration</span><span class="p">);</span>

        <span class="c1">//如果它之前被调用过至少一次，则进行更复杂的计算</span>
        <span class="k">if</span> <span class="p">(</span><span class="nx">functionData</span><span class="p">.</span><span class="nx">calls</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
            <span class="nx">functionData</span><span class="p">.</span><span class="nx">avg</span> <span class="o">=</span> <span class="p">((</span><span class="nx">functionData</span><span class="p">.</span><span class="nx">avg</span><span class="o">*</span><span class="p">(</span><span class="nx">functionData</span><span class="p">.</span><span class="nx">calls</span><span class="o">-</span><span class="mi">1</span><span class="p">))</span><span class="o">+</span><span class="nx">duration</span><span class="p">)</span><span class="o">/</span><span class="nx">functionData</span><span class="p">.</span><span class="nx">calls</span><span class="p">;</span>
            <span class="nx">functionData</span><span class="p">.</span><span class="nx">min</span> <span class="o">=</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">min</span><span class="p">(</span><span class="nx">functionData</span><span class="p">.</span><span class="nx">min</span><span class="p">,</span> <span class="nx">duration</span><span class="p">);</span>
            <span class="nx">functionData</span><span class="p">.</span><span class="nx">max</span> <span class="o">=</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">max</span><span class="p">(</span><span class="nx">functionData</span><span class="p">.</span><span class="nx">max</span><span class="p">,</span> <span class="nx">duration</span><span class="p">);</span>
        <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
            <span class="nx">functionData</span><span class="p">.</span><span class="nx">avg</span> <span class="o">=</span> <span class="nx">duration</span><span class="p">;</span>
            <span class="nx">functionData</span><span class="p">.</span><span class="nx">min</span> <span class="o">=</span> <span class="nx">duration</span><span class="p">;</span>
            <span class="nx">functionData</span><span class="p">.</span><span class="nx">max</span> <span class="o">=</span> <span class="nx">duration</span><span class="p">;</span>
        <span class="p">}</span>                             
    
    <span class="p">},</span>

    <span class="c1">//-------------------------------------------------------------------------</span>
    <span class="c1">// 报表方法</span>
    <span class="c1">//-------------------------------------------------------------------------    </span>
    
    <span class="cm">/**</span>
<span class="cm">     * 返回指定函数执行所耗费的平均时间（单位：毫秒）。</span>
<span class="cm">     * @param {String} name 函数名称，本方法将返回该函数的数据。</span>
<span class="cm">     *      如果该“函数”是一个对象类型（译注：原型）的方法，</span>
<span class="cm">     *      则其名称应取自 &#39;constructor.prototype.methodName&#39;；</span>
<span class="cm">     *      而一个普通对象的方法名称将取自 &#39;object.methodName&#39;。</span>
<span class="cm">     * @return {float} 该函数执行所耗费的平均时间。</span>
<span class="cm">     * @static</span>
<span class="cm">     */</span>
    <span class="nx">getAverage</span> <span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">name</span> <span class="cm">/*:String*/</span><span class="p">)</span> <span class="cm">/*:float*/</span> <span class="p">{</span>
        <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_report</span><span class="p">[</span><span class="nx">name</span><span class="p">].</span><span class="nx">avg</span><span class="p">;</span>
    <span class="p">},</span>

    <span class="cm">/**</span>
<span class="cm">     * 返回指定函数的执行次数。</span>
<span class="cm">     * @param {String} name 函数名称，本方法将返回该函数的数据。</span>
<span class="cm">     * @return {int} 该函数被执行的次数。</span>
<span class="cm">     * @static</span>
<span class="cm">     */</span>
    <span class="nx">getCallCount</span> <span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">name</span> <span class="cm">/*:String*/</span><span class="p">)</span> <span class="cm">/*:int*/</span> <span class="p">{</span>
        <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_report</span><span class="p">[</span><span class="nx">name</span><span class="p">].</span><span class="nx">calls</span><span class="p">;</span>    
    <span class="p">},</span>
    
    <span class="cm">/**</span>
<span class="cm">     * 返回指定函数执行所耗费的最长时间（单位：毫秒）。</span>
<span class="cm">     * @param {String} name 函数名称，本方法将返回该函数的数据。</span>
<span class="cm">     *      如果该“函数”是一个对象类型（译注：原型）的方法，</span>
<span class="cm">     *      则其名称应取自 &#39;constructor.prototype.methodName&#39;；</span>
<span class="cm">     *      而一个普通对象的方法名称将取自 &#39;object.methodName&#39;。</span>
<span class="cm">     * @return {float} 该函数执行所耗费的最长时间。</span>
<span class="cm">     */</span>
    <span class="nx">getMax</span> <span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">name</span> <span class="cm">/*:String*/</span><span class="p">)</span> <span class="cm">/*:int*/</span> <span class="p">{</span>
        <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_report</span><span class="p">[</span><span class="nx">name</span><span class="p">].</span><span class="nx">max</span><span class="p">;</span>
    <span class="p">},</span>
    
    <span class="cm">/**</span>
<span class="cm">     * 返回指定函数执行所耗费的最短时间（单位：毫秒）。</span>
<span class="cm">     * @param {String} name 函数名称，本方法将返回该函数的数据。</span>
<span class="cm">     *      如果该“函数”是一个对象类型（译注：原型）的方法，</span>
<span class="cm">     *      则其名称应取自 &#39;constructor.prototype.methodName&#39;；</span>
<span class="cm">     *      而一个普通对象的方法名称将取自 &#39;object.methodName&#39;。</span>
<span class="cm">     * @return {float} 该函数执行所耗费的最短时间。</span>
<span class="cm">     */</span>
    <span class="nx">getMin</span> <span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">name</span> <span class="cm">/*:String*/</span><span class="p">)</span> <span class="cm">/*:int*/</span> <span class="p">{</span>
        <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_report</span><span class="p">[</span><span class="nx">name</span><span class="p">].</span><span class="nx">min</span><span class="p">;</span>
    <span class="p">},</span>

    <span class="cm">/**</span>
<span class="cm">     * 返回一个存有某函数性能评测数据的对象。</span>
<span class="cm">     * 该对象有一份包含该函数最短、最长和平均耗时，还有执行次数和评分等数据的记录。</span>
<span class="cm">     * @return {Object} 一个存有指定函数性能评测数据的对象。</span>
<span class="cm">     * @static</span>
<span class="cm">     */</span>
    <span class="nx">getFunctionReport</span> <span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">name</span> <span class="cm">/*:String*/</span><span class="p">)</span> <span class="cm">/*:Object*/</span> <span class="p">{</span>
        <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_report</span><span class="p">[</span><span class="nx">name</span><span class="p">];</span>
    <span class="p">},</span>

    <span class="cm">/**</span>
<span class="cm">     * 返回一个存有所有已被评测函数数据的对象。</span>
<span class="cm">     * 该对象存有每个函数的评测记录，并返回其所有信息，</span>
<span class="cm">     * 包括最短、最长和平均耗时和执行次数等数据。</span>
<span class="cm">     * @return {Object} 一个存有所有性能评测数据的对象。</span>
<span class="cm">     * @static</span>
<span class="cm">     */</span>
    <span class="nx">getFullReport</span> <span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">filter</span> <span class="cm">/*:Function*/</span><span class="p">)</span> <span class="cm">/*:Object*/</span> <span class="p">{</span>
        <span class="nx">filter</span> <span class="o">=</span> <span class="nx">filter</span> <span class="o">||</span> <span class="kd">function</span><span class="p">(){</span><span class="k">return</span> <span class="kc">true</span><span class="p">;};</span>
    
        <span class="k">if</span> <span class="p">(</span><span class="nx">YAHOO</span><span class="p">.</span><span class="nx">lang</span><span class="p">.</span><span class="nx">isFunction</span><span class="p">(</span><span class="nx">filter</span><span class="p">))</span> <span class="p">{</span>
            <span class="kd">var</span> <span class="nx">report</span> <span class="o">=</span> <span class="p">{};</span>
            
            <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">name</span> <span class="k">in</span> <span class="k">this</span><span class="p">.</span><span class="nx">_report</span><span class="p">){</span>
                <span class="k">if</span> <span class="p">(</span><span class="nx">filter</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_report</span><span class="p">[</span><span class="nx">name</span><span class="p">])){</span>
                    <span class="nx">report</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_report</span><span class="p">[</span><span class="nx">name</span><span class="p">];</span>    
                <span class="p">}</span>
            <span class="p">}</span>
            
            <span class="k">return</span> <span class="nx">report</span><span class="p">;</span>
        <span class="p">}</span>
    <span class="p">},</span>

    <span class="c1">//-------------------------------------------------------------------------</span>
    <span class="c1">// 性能评测方法</span>
    <span class="c1">//-------------------------------------------------------------------------   </span>
    
    <span class="cm">/**</span>
<span class="cm">     * 创建一个构造器，包含原型的所有属性和方法。</span>
<span class="cm">     * @param {string} name 包含命名空间信息的完整函数名称.</span>
<span class="cm">     * @param {Object} owner (可选) 该函数的宿主对象（命名空间或者其父级对象）。</span>
<span class="cm">     * @return {Void}</span>
<span class="cm">     * @static</span>
<span class="cm">     */</span>
    <span class="nx">registerConstructor</span> <span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">name</span> <span class="cm">/*:String*/</span><span class="p">,</span> <span class="nx">owner</span> <span class="cm">/*:Object*/</span><span class="p">)</span> <span class="cm">/*:Void*/</span> <span class="p">{</span>    
        <span class="k">this</span><span class="p">.</span><span class="nx">registerFunction</span><span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">owner</span><span class="p">,</span> <span class="kc">true</span><span class="p">);</span>
    <span class="p">},</span>

    <span class="cm">/**</span>
<span class="cm">     * 创建一个用于性能测试的函数。</span>
<span class="cm">     * 本方法将用一个包含检测数据的函数覆写原有函数。</span>
<span class="cm">     * 它还将为该函数创建一份性能评测报表。</span>
<span class="cm">     * 原始函数将被存到 _container 对象里.</span>
<span class="cm">     * @param {String} name 包含命名空间的完整函数名称。</span>
<span class="cm">     *      该名称将被存入性能测试报表。</span>
<span class="cm">     * @param {Object} owner (可选) 该函数的宿主对象。</span>
<span class="cm">     *      如果该函数非全局函数，则该参数必填。</span>
<span class="cm">     *      其值可以是该函数所属的命名空间，比如 YAHOO.util.Dom，</span>
<span class="cm">     *      而如果它是一个方法则该值为其父级对象。</span>
<span class="cm">     * @return {Void}</span>
<span class="cm">     * @method registerFunction</span>
<span class="cm">     */</span>     
    <span class="nx">registerFunction</span> <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">name</span> <span class="cm">/*:String*/</span><span class="p">,</span> <span class="nx">owner</span> <span class="cm">/*:Object*/</span><span class="p">,</span> <span class="nx">registerPrototype</span> <span class="cm">/*:Boolean*/</span><span class="p">)</span> <span class="cm">/*:Void*/</span><span class="p">{</span>
    
        <span class="c1">//解析出不含命名空间的函数名称</span>
        <span class="kd">var</span> <span class="nx">funcName</span> <span class="cm">/*:String*/</span> <span class="o">=</span> <span class="p">(</span><span class="nx">name</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="s2">&quot;.&quot;</span><span class="p">)</span> <span class="o">&gt;</span> <span class="o">-</span><span class="mi">1</span> <span class="o">?</span> <span class="nx">name</span><span class="p">.</span><span class="nx">substring</span><span class="p">(</span><span class="nx">name</span><span class="p">.</span><span class="nx">lastIndexOf</span><span class="p">(</span><span class="s2">&quot;.&quot;</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="o">:</span> <span class="nx">name</span><span class="p">);</span>
        <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">YAHOO</span><span class="p">.</span><span class="nx">lang</span><span class="p">.</span><span class="nx">isObject</span><span class="p">(</span><span class="nx">owner</span><span class="p">)){</span>
            <span class="nx">owner</span> <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="nx">name</span><span class="p">.</span><span class="nx">substring</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nx">name</span><span class="p">.</span><span class="nx">lastIndexOf</span><span class="p">(</span><span class="s2">&quot;.&quot;</span><span class="p">)));</span>
        <span class="p">}</span>
        
        <span class="c1">//获取方法及其原型</span>
        <span class="kd">var</span> <span class="nx">method</span> <span class="cm">/*:Function*/</span> <span class="o">=</span> <span class="nx">owner</span><span class="p">[</span><span class="nx">funcName</span><span class="p">];</span>
        <span class="kd">var</span> <span class="nx">prototype</span> <span class="cm">/*:Object*/</span> <span class="o">=</span> <span class="nx">method</span><span class="p">.</span><span class="nx">prototype</span><span class="p">;</span>
        
        <span class="c1">//检查该方法是否已被注册</span>
        <span class="k">if</span> <span class="p">(</span><span class="nx">YAHOO</span><span class="p">.</span><span class="nx">lang</span><span class="p">.</span><span class="nx">isFunction</span><span class="p">(</span><span class="nx">method</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="nx">method</span><span class="p">.</span><span class="nx">__yuiProfiled</span><span class="p">){</span>
            
            <span class="c1">//为原始方法新建一个备份</span>
            <span class="k">this</span><span class="p">.</span><span class="nx">_container</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">method</span><span class="p">;</span>
            
            <span class="c1">//将该函数替换成包含性能测试数据的版本</span>
            <span class="nx">owner</span><span class="p">[</span><span class="nx">funcName</span><span class="p">]</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>

                <span class="kd">var</span> <span class="nx">start</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">Date</span><span class="p">();</span>     
                <span class="kd">var</span> <span class="nx">retval</span> <span class="o">=</span> <span class="nx">method</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="nx">arguments</span><span class="p">);</span>
                <span class="kd">var</span> <span class="nx">stop</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">Date</span><span class="p">();</span>
                
                <span class="nx">YAHOO</span><span class="p">.</span><span class="nx">tool</span><span class="p">.</span><span class="nx">Profiler</span><span class="p">.</span><span class="nx">_saveData</span><span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">stop</span><span class="o">-</span><span class="nx">start</span><span class="p">);</span>
                
                <span class="k">return</span> <span class="nx">retval</span><span class="p">;</span>                
            
            <span class="p">};</span>
            
            <span class="c1">//附上函数的属性</span>
            <span class="nx">YAHOO</span><span class="p">.</span><span class="nx">lang</span><span class="p">.</span><span class="nx">augmentObject</span><span class="p">(</span><span class="nx">owner</span><span class="p">[</span><span class="nx">funcName</span><span class="p">],</span> <span class="nx">method</span><span class="p">);</span>
            <span class="nx">owner</span><span class="p">[</span><span class="nx">funcName</span><span class="p">].</span><span class="nx">__yuiProfiled</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
            <span class="nx">owner</span><span class="p">[</span><span class="nx">funcName</span><span class="p">].</span><span class="nx">prototype</span> <span class="o">=</span> <span class="nx">prototype</span><span class="p">;</span>
            <span class="k">this</span><span class="p">.</span><span class="nx">_container</span><span class="p">[</span><span class="nx">name</span><span class="p">].</span><span class="nx">__yuiOwner</span> <span class="o">=</span> <span class="nx">owner</span><span class="p">;</span>
            <span class="k">this</span><span class="p">.</span><span class="nx">_container</span><span class="p">[</span><span class="nx">name</span><span class="p">].</span><span class="nx">__yuiFuncName</span> <span class="o">=</span> <span class="nx">funcName</span><span class="p">;</span>        
        
            <span class="c1">//如果有必要，注册原型</span>
            <span class="k">if</span> <span class="p">(</span><span class="nx">registerPrototype</span><span class="p">)</span> <span class="p">{</span>            
                <span class="k">this</span><span class="p">.</span><span class="nx">registerObject</span><span class="p">(</span><span class="nx">name</span> <span class="o">+</span> <span class="s2">&quot;.prototype&quot;</span><span class="p">,</span> <span class="nx">prototype</span><span class="p">);</span>          
            <span class="p">}</span>
            
            <span class="c1">//保存函数信息</span>
            <span class="k">this</span><span class="p">.</span><span class="nx">_report</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span>
                <span class="nx">calls</span><span class="o">:</span> <span class="mi">0</span><span class="p">,</span>
                <span class="nx">max</span><span class="o">:</span> <span class="mi">0</span><span class="p">,</span>
                <span class="nx">min</span><span class="o">:</span> <span class="mi">0</span><span class="p">,</span>
                <span class="nx">avg</span><span class="o">:</span> <span class="mi">0</span><span class="p">,</span>
                <span class="nx">points</span><span class="o">:</span> <span class="p">[]</span>
            <span class="p">};</span>        
        <span class="p">}</span>
            
        <span class="k">return</span> <span class="nx">method</span><span class="p">;</span>
    
    <span class="p">},</span>
        
    
    <span class="cm">/**</span>
<span class="cm">     * 创建一个用于性能测试的对象。</span>
<span class="cm">     * 它对对象进行操作，并寻找其子方法。</span>
<span class="cm">     * 当找到一个子方法，将对其调用 registerFunction()。</span>
<span class="cm">     * 如果设置为递归模式，它还将为所找到的子对象以相同的方法创建用于性能测试的对象。</span>
<span class="cm">     * @param {String} name 用来接受性能测试的对象名称（在报表里显示）。</span>
<span class="cm">     * @param {Object} owner (可选) 以该名称被声明的对象本身。</span>
<span class="cm">     * @param {Boolean} recurse (可选) 该参数决定是否子对象方法也需要参与性能测试。</span>
<span class="cm">     * @return {Void}</span>
<span class="cm">     * @static</span>
<span class="cm">     */</span>
    <span class="nx">registerObject</span> <span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">name</span> <span class="cm">/*:String*/</span><span class="p">,</span> <span class="nx">object</span> <span class="cm">/*:Object*/</span><span class="p">,</span> <span class="nx">recurse</span> <span class="cm">/*:Boolean*/</span><span class="p">)</span> <span class="cm">/*:Void*/</span><span class="p">{</span>
    
        <span class="c1">//获取该对象</span>
        <span class="nx">object</span> <span class="o">=</span> <span class="p">(</span><span class="nx">YAHOO</span><span class="p">.</span><span class="nx">lang</span><span class="p">.</span><span class="nx">isObject</span><span class="p">(</span><span class="nx">object</span><span class="p">)</span> <span class="o">?</span> <span class="nx">object</span> <span class="o">:</span> <span class="nb">eval</span><span class="p">(</span><span class="nx">name</span><span class="p">));</span>
    
        <span class="c1">//备份该对象</span>
        <span class="k">this</span><span class="p">.</span><span class="nx">_container</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">object</span><span class="p">;</span>
    
        <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">prop</span> <span class="k">in</span> <span class="nx">object</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">object</span><span class="p">[</span><span class="nx">prop</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;function&quot;</span><span class="p">){</span>
                <span class="k">if</span> <span class="p">(</span><span class="nx">prop</span> <span class="o">!=</span> <span class="s2">&quot;constructor&quot;</span> <span class="o">&amp;&amp;</span> <span class="nx">prop</span> <span class="o">!=</span> <span class="s2">&quot;superclass&quot;</span><span class="p">){</span> <span class="c1">//不要操作构造器或超类，否则将无限递归</span>
                    <span class="k">this</span><span class="p">.</span><span class="nx">registerFunction</span><span class="p">(</span><span class="nx">name</span> <span class="o">+</span> <span class="s2">&quot;.&quot;</span> <span class="o">+</span> <span class="nx">prop</span><span class="p">,</span> <span class="nx">object</span><span class="p">);</span>
                <span class="p">}</span>
            <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">object</span><span class="p">[</span><span class="nx">prop</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;object&quot;</span> <span class="o">&amp;&amp;</span> <span class="nx">recurse</span><span class="p">){</span>
                <span class="k">this</span><span class="p">.</span><span class="nx">registerObject</span><span class="p">(</span><span class="nx">name</span> <span class="o">+</span> <span class="s2">&quot;.&quot;</span> <span class="o">+</span> <span class="nx">prop</span><span class="p">,</span> <span class="nx">object</span><span class="p">[</span><span class="nx">prop</span><span class="p">],</span> <span class="nx">recurse</span><span class="p">);</span>
            <span class="p">}</span>
        <span class="p">}</span>
    
    <span class="p">},</span>    
    
    <span class="cm">/**</span>
<span class="cm">     * 从性能测试器里注销构造器。registerConstructor() 方法的逆操作。</span>
<span class="cm">     * @param {String} name 包含命名空间的完整函数名称。</span>
<span class="cm">     *      该名称将被存入性能测试报表。</span>
<span class="cm">     * @return {Void}</span>
<span class="cm">     * @method unregisterFunction</span>
<span class="cm">     */</span>     
    <span class="nx">unregisterConstructor</span> <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">name</span> <span class="cm">/*:String*/</span><span class="p">)</span> <span class="cm">/*:Void*/</span><span class="p">{</span>
            
        <span class="c1">//检查是否该方法已被注册</span>
        <span class="k">if</span> <span class="p">(</span><span class="nx">YAHOO</span><span class="p">.</span><span class="nx">lang</span><span class="p">.</span><span class="nx">isFunction</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_container</span><span class="p">[</span><span class="nx">name</span><span class="p">])){</span>
        
            <span class="c1">//获得原始数据</span>
            <span class="c1">//var owner /*:Object*/ = this._container[name].__yuiOwner;</span>
            <span class="c1">//var funcName /*:String*/ = this._container[name].__yuiFuncName;</span>
            <span class="c1">//delete this._container[name].__yuiOwner;</span>
            <span class="c1">//delete this._container[name].__yuiFuncName;</span>
            
            <span class="c1">//还原性能测试函数</span>
            <span class="c1">//owner[funcName] = this._container[name];</span>
            <span class="c1">//delete this._container[name];</span>
            <span class="k">this</span><span class="p">.</span><span class="nx">unregisterFunction</span><span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="kc">true</span><span class="p">);</span>
       
        <span class="p">}</span>

    
    <span class="p">},</span>
    
    <span class="cm">/**</span>
<span class="cm">     * 从性能测试器里注销构造器。registerFunction() 方法的逆操作。</span>
<span class="cm">     * @param {String} name 包含命名空间的完整函数名称。</span>
<span class="cm">     *      该名称将被存入性能测试报表。</span>
<span class="cm">     * @return {Void}</span>
<span class="cm">     * @method unregisterFunction</span>
<span class="cm">     */</span>     
    <span class="nx">unregisterFunction</span> <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">name</span> <span class="cm">/*:String*/</span><span class="p">,</span> <span class="nx">unregisterPrototype</span> <span class="cm">/*:Boolean*/</span><span class="p">)</span> <span class="cm">/*:Void*/</span><span class="p">{</span>
            
        <span class="c1">//检查是否该方法已被注册</span>
        <span class="k">if</span> <span class="p">(</span><span class="nx">YAHOO</span><span class="p">.</span><span class="nx">lang</span><span class="p">.</span><span class="nx">isFunction</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_container</span><span class="p">[</span><span class="nx">name</span><span class="p">])){</span>
        
            <span class="c1">//检查是否需要注销原型</span>
            <span class="k">if</span> <span class="p">(</span><span class="nx">unregisterPrototype</span><span class="p">){</span>
                <span class="k">this</span><span class="p">.</span><span class="nx">unregisterObject</span><span class="p">(</span><span class="nx">name</span> <span class="o">+</span> <span class="s2">&quot;.prototype&quot;</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">_container</span><span class="p">[</span><span class="nx">name</span><span class="p">].</span><span class="nx">prototype</span><span class="p">);</span>
            <span class="p">}</span>
                
            <span class="c1">//获得原始数据</span>
            <span class="kd">var</span> <span class="nx">owner</span> <span class="cm">/*:Object*/</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_container</span><span class="p">[</span><span class="nx">name</span><span class="p">].</span><span class="nx">__yuiOwner</span><span class="p">;</span>
            <span class="kd">var</span> <span class="nx">funcName</span> <span class="cm">/*:String*/</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_container</span><span class="p">[</span><span class="nx">name</span><span class="p">].</span><span class="nx">__yuiFuncName</span><span class="p">;</span>
            <span class="k">delete</span> <span class="k">this</span><span class="p">.</span><span class="nx">_container</span><span class="p">[</span><span class="nx">name</span><span class="p">].</span><span class="nx">__yuiOwner</span><span class="p">;</span>
            <span class="k">delete</span> <span class="k">this</span><span class="p">.</span><span class="nx">_container</span><span class="p">[</span><span class="nx">name</span><span class="p">].</span><span class="nx">__yuiFuncName</span><span class="p">;</span>
            
            <span class="c1">//还原性能测试函数</span>
            <span class="nx">owner</span><span class="p">[</span><span class="nx">funcName</span><span class="p">]</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_container</span><span class="p">[</span><span class="nx">name</span><span class="p">];</span>
            
            <span class="c1">//删除辅助信息</span>
            <span class="k">delete</span> <span class="k">this</span><span class="p">.</span><span class="nx">_container</span><span class="p">[</span><span class="nx">name</span><span class="p">];</span>
            <span class="k">delete</span> <span class="k">this</span><span class="p">.</span><span class="nx">_report</span><span class="p">[</span><span class="nx">name</span><span class="p">];</span>
       
        <span class="p">}</span>
            
    
    <span class="p">},</span>
    
    <span class="cm">/**</span>
<span class="cm">     * 从性能测试器里注销对象。</span>
<span class="cm">     * 它对对象进行操作，并寻找其子方法。</span>
<span class="cm">     * 当找到一个子方法，将对其调用 unregisterFunction()。</span>
<span class="cm">     * 如果设置为递归模式，它还将为所找到的子对象以相同的方法创建用于性能测试的对象。</span>
<span class="cm">     * @param {String} name 对象名称，该对象将从性能测试器里注销。</span>
<span class="cm">     * @param {Boolean} recurse (可选) 该参数决定是否子对象方法也需要被注销。</span>
<span class="cm">     * @return {Void}</span>
<span class="cm">     * @static</span>
<span class="cm">     */</span>
    <span class="nx">unregisterObject</span> <span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">name</span> <span class="cm">/*:String*/</span><span class="p">,</span> <span class="nx">recurse</span> <span class="cm">/*:Boolean*/</span><span class="p">)</span> <span class="cm">/*:Void*/</span><span class="p">{</span>
    
        <span class="c1">//获取该对象</span>
        <span class="k">if</span> <span class="p">(</span><span class="nx">YAHOO</span><span class="p">.</span><span class="nx">lang</span><span class="p">.</span><span class="nx">isObject</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">_container</span><span class="p">[</span><span class="nx">name</span><span class="p">])){</span>            
            <span class="kd">var</span> <span class="nx">object</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">_container</span><span class="p">[</span><span class="nx">name</span><span class="p">];</span>    
        
            <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">prop</span> <span class="k">in</span> <span class="nx">object</span><span class="p">)</span> <span class="p">{</span>
                <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">object</span><span class="p">[</span><span class="nx">prop</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;function&quot;</span><span class="p">){</span>
                    <span class="k">this</span><span class="p">.</span><span class="nx">unregisterFunction</span><span class="p">(</span><span class="nx">name</span> <span class="o">+</span> <span class="s2">&quot;.&quot;</span> <span class="o">+</span> <span class="nx">prop</span><span class="p">);</span>
                <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">object</span><span class="p">[</span><span class="nx">prop</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;object&quot;</span> <span class="o">&amp;&amp;</span> <span class="nx">recurse</span><span class="p">){</span>
                    <span class="k">this</span><span class="p">.</span><span class="nx">unregisterObject</span><span class="p">(</span><span class="nx">name</span> <span class="o">+</span> <span class="s2">&quot;.&quot;</span> <span class="o">+</span> <span class="nx">prop</span><span class="p">,</span> <span class="nx">recurse</span><span class="p">);</span>
                <span class="p">}</span>
            <span class="p">}</span>
            
            <span class="k">delete</span> <span class="k">this</span><span class="p">.</span><span class="nx">_container</span><span class="p">[</span><span class="nx">name</span><span class="p">];</span>
        <span class="p">}</span>
    
    <span class="p">}</span>
         

<span class="p">};</span>
<span class="nx">YAHOO</span><span class="p">.</span><span class="nx">register</span><span class="p">(</span><span class="s2">&quot;profiler&quot;</span><span class="p">,</span> <span class="nx">YAHOO</span><span class="p">.</span><span class="nx">tool</span><span class="p">.</span><span class="nx">Profiler</span><span class="p">,</span> <span class="p">{</span><span class="nx">version</span><span class="o">:</span> <span class="s2">&quot;2.7.0&quot;</span><span class="p">,</span> <span class="nx">build</span><span class="o">:</span> <span class="s2">&quot;1799&quot;</span><span class="p">});</span>
</pre></div>
                    </div>
			</div>
		</div>
		<div class="yui-b">
            <div class="nav">

                    <div id="moduleList" class="module">
                        <h4>模块</h4>
                        <ul class="content">
                                <li class=""><a href="module_animation.html" title="animation">animation</a></li>
                                <li class=""><a href="module_autocomplete.html" title="autocomplete">autocomplete</a></li>
                                <li class=""><a href="module_button.html" title="button">button</a></li>
                                <li class=""><a href="module_calendar.html" title="calendar">calendar</a></li>
                                <li class=""><a href="module_carousel.html" title="carousel">carousel</a></li>
                                <li class=""><a href="module_charts.html" title="charts">charts</a></li>
                                <li class=""><a href="module_colorpicker.html" title="colorpicker">colorpicker</a></li>
                                <li class=""><a href="module_connection.html" title="connection">connection</a></li>
                                <li class=""><a href="module_container.html" title="container">container</a></li>
                                <li class=""><a href="module_cookie.html" title="cookie">cookie</a></li>
                                <li class=""><a href="module_datasource.html" title="datasource">datasource</a></li>
                                <li class=""><a href="module_datatable.html" title="datatable">datatable</a></li>
                                <li class=""><a href="module_datemath.html" title="datemath">datemath</a></li>
                                <li class=""><a href="module_dom.html" title="dom">dom</a></li>
                                <li class=""><a href="module_dragdrop.html" title="dragdrop">dragdrop</a></li>
                                <li class=""><a href="module_editor.html" title="editor">editor</a></li>
                                <li class=""><a href="module_element.html" title="element">element</a></li>
                                <li class=""><a href="module_event.html" title="event">event</a></li>
                                <li class=""><a href="module_get.html" title="get">get</a></li>
                                <li class=""><a href="module_history.html" title="history">history</a></li>
                                <li class=""><a href="module_imagecropper.html" title="imagecropper">imagecropper</a></li>
                                <li class=""><a href="module_imageloader.html" title="imageloader">imageloader</a></li>
                                <li class=""><a href="module_json.html" title="json">json</a></li>
                                <li class=""><a href="module_layout.html" title="layout">layout</a></li>
                                <li class=""><a href="module_logger.html" title="logger">logger</a></li>
                                <li class=""><a href="module_menu.html" title="menu">menu</a></li>
                                <li class=""><a href="module_paginator.html" title="paginator">paginator</a></li>
                                <li class="selected"><a href="module_profiler.html" title="profiler">profiler</a></li>
                                <li class=""><a href="module_profilerviewer.html" title="profilerviewer">profilerviewer</a></li>
                                <li class=""><a href="module_resize.html" title="resize">resize</a></li>
                                <li class=""><a href="module_selector.html" title="selector">selector</a></li>
                                <li class=""><a href="module_slider.html" title="slider">slider</a></li>
                                <li class=""><a href="module_stylesheet.html" title="stylesheet">stylesheet</a></li>
                                <li class=""><a href="module_tabview.html" title="tabview">tabview</a></li>
                                <li class=""><a href="module_treeview.html" title="treeview">treeview</a></li>
                                <li class=""><a href="module_uploader.html" title="uploader">uploader</a></li>
                                <li class=""><a href="module_yahoo.html" title="yahoo">yahoo</a></li>
                                <li class=""><a href="module_yuiloader.html" title="yuiloader">yuiloader</a></li>
                                <li class=""><a href="module_yuitest.html" title="yuitest">yuitest</a></li>
                        </ul>
                    </div>

                    <div id="classList" class="module">
                        <h4>类</h4>
                        <ul class="content">
                                <li class=""><a href="YAHOO.tool.Profiler.html" title="YAHOO.tool.Profiler">YAHOO.tool.Profiler</a></li>
                        </ul>
                    </div>

                    <div id="fileList" class="module">
                        <h4>文件</h4>
                        <ul class="content">        
                                <li class="selected"><a href="profiler.js.html" title="profiler.js">profiler.js</a></li>
                        </ul>
                    </div>





            </div>
		</div>
	</div>
	<div id="ft">
        <hr />
        Copyright &copy; 2009 Yahoo! Inc. All rights reserved.
	</div>
</div>
<script type="text/javascript">
    ALL_YUI_PROPS = [{"url": "YAHOO.tool.Profiler.html#method_getAverage", "access": "", "host": "YAHOO.tool.Profiler", "type": "method", "name": "getAverage"}, {"url": "YAHOO.tool.Profiler.html#method_getCallCount", "access": "", "host": "YAHOO.tool.Profiler", "type": "method", "name": "getCallCount"}, {"url": "YAHOO.tool.Profiler.html#method_getFullReport", "access": "", "host": "YAHOO.tool.Profiler", "type": "method", "name": "getFullReport"}, {"url": "YAHOO.tool.Profiler.html#method_getFunctionReport", "access": "", "host": "YAHOO.tool.Profiler", "type": "method", "name": "getFunctionReport"}, {"url": "YAHOO.tool.Profiler.html#method_getMax", "access": "", "host": "YAHOO.tool.Profiler", "type": "method", "name": "getMax"}, {"url": "YAHOO.tool.Profiler.html#method_getMin", "access": "", "host": "YAHOO.tool.Profiler", "type": "method", "name": "getMin"}, {"url": "YAHOO.tool.Profiler.html#method_registerConstructor", "access": "", "host": "YAHOO.tool.Profiler", "type": "method", "name": "registerConstructor"}, {"url": "YAHOO.tool.Profiler.html#method_registerFunction", "access": "", "host": "YAHOO.tool.Profiler", "type": "method", "name": "registerFunction"}, {"url": "YAHOO.tool.Profiler.html#method_registerObject", "access": "", "host": "YAHOO.tool.Profiler", "type": "method", "name": "registerObject"}, {"url": "YAHOO.tool.Profiler.html#method_unregisterFunction", "access": "", "host": "YAHOO.tool.Profiler", "type": "method", "name": "unregisterFunction"}, {"url": "YAHOO.tool.Profiler.html#method_unregisterObject", "access": "", "host": "YAHOO.tool.Profiler", "type": "method", "name": "unregisterObject"}];
</script>
</body>
</html>
